package com.steven.sample.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.steven.sample.R;

import java.util.ArrayList;

/**
 * description: TestView
 * author: Steven Xiao
 * date: 2017/3/9
 */
public class TestView extends View {
    private static final String TAG = TestView.class.getSimpleName();

    private int mBgColor = Color.GRAY;
    private int mCurrentColor = Color.GREEN;

    private int mBarHeight = 20;

    private int mTriHeght = 20;
    private int mTriWidth = 20;

    private int mHintTextColor = Color.BLACK;
    private int mHintTextSize = 12;

    private int mSegmentNumb = 5;
    private int mSegmentWidth = 1080 / 4;
    private ArrayList<String> mUnitList;


    public TestView(Context context) {
        super(context);
    }

    public TestView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TestView);
        if (array != null) {
            mBgColor = array.getColor(R.styleable.TestView_bar_bg_color, Color.GRAY);
            mCurrentColor = array.getColor(R.styleable.TestView_bar_front_color, Color.BLACK);

            mTriWidth = array.getDimensionPixelSize(R.styleable.TestView_triangle_width, 20);
            mTriHeght = array.getDimensionPixelSize(R.styleable.TestView_triangle_height, 20);
            mBarHeight = array.getDimensionPixelSize(R.styleable.TestView_bar_height, 20);

            mHintTextColor = array.getColor(R.styleable.TestView_hint_text_color, Color.BLACK);
            mHintTextSize = array.getDimensionPixelSize(R.styleable.TestView_hint_text_size, 12);
        }
    }

    public TestView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public TestView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int nextHeight = 0;

        int textPadding = getWidth() / 5;

        Paint paint = new Paint();
        //上面的文字
        paint.setColor(mHintTextColor);
        paint.setTextSize(mHintTextSize);
        paint.setAntiAlias(true);
        String text = "每单10%-20%成单金额奖励";
        canvas.drawText(text, 0, 2, textPadding - mTriWidth / 2, mHintTextSize, paint);
        canvas.drawText(text, 9, text.length(), textPadding - mTriWidth / 2 + getTextWidth(paint,"每单10%-20%"), mHintTextSize, paint);
        paint.setColor(Color.RED);
        canvas.drawText(text, 2, 9, textPadding - mTriWidth / 2 + getTextWidth(paint,"每单"), mHintTextSize, paint);
        Log.e(TAG,"length = "+getTextWidth(paint,"每单"));
        Log.e(TAG,"length = "+getTextWidth(paint,"10%-20%"));

        nextHeight = mHintTextSize + 15;

        //三角 设置paint 的style为 FILL：实心
        paint.setColor(mCurrentColor);
        paint.setStyle(Paint.Style.FILL);
        Path path = new Path();
        path.moveTo(textPadding - mTriWidth / 2, nextHeight);
        path.lineTo(mTriWidth + textPadding - mTriWidth / 2, nextHeight);
        path.lineTo(mTriWidth / 2 + textPadding - mTriWidth / 2, mTriHeght + nextHeight);
        path.close();
        canvas.drawPath(path, paint);

        nextHeight = nextHeight + mTriHeght;

        paint.setColor(mBgColor);
        Rect bgRect = new Rect(0, nextHeight, mSegmentWidth * mSegmentNumb, nextHeight + mBarHeight);
        canvas.drawRect(bgRect, paint);

        Rect currentRect = new Rect(0, nextHeight, getWidth() / 5, nextHeight + mBarHeight);
        paint.setColor(mCurrentColor);
        canvas.drawRect(currentRect, paint);

        nextHeight = nextHeight + mBarHeight + 20 + mHintTextSize;
        paint.setColor(mBgColor);
//        paint.setTextAlign(Align.CENTER);
        for (int i = 0; i < mSegmentNumb; i++) {
            canvas.drawText(mUnitList.get(i), i * mSegmentWidth, nextHeight, paint);
        }
    }

    public static int getTextWidth(Paint paint, String str) {
        int length = 0;
        if (str != null && str.length() > 0) {
            int len = str.length();
            float[] widths = new float[len];
            paint.getTextWidths(str, widths);
            for (int j = 0; j < len; j++) {
                length += (int) Math.ceil(widths[j]);
            }
        }
        return length;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.makeMeasureSpec(mSegmentWidth * mSegmentNumb, MeasureSpec.EXACTLY);
        super.onMeasure(width, heightMeasureSpec);
    }

    public void setSegmentNum(int num) {
        this.mSegmentNumb = num;
    }

    public void setUnitList(ArrayList<String> list) {
        mUnitList = list;
    }


}
